package com.hunk.springMvcExercise.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hunk.core.auth.entity.User;
import com.hunk.utils.PropertyHelper;

/**返回为jsp的controller类
 * @author HunkDeng
 *
 */
@Controller
@RequestMapping("/prop")
public class PropertyConroller {
	public final static String folder = "testProperty/";
	
	/**返回jsp页面
	 * @return
	 */
	@RequestMapping(value="/index")
	public String index() {
		return folder + "prop";
	}

	/**处理form表单并返回success页面或failed页面
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/edit",method=RequestMethod.POST)
	public ModelAndView login(@ModelAttribute("user")User user,
			HttpServletRequest request) {
		PropertyHelper ph = PropertyHelper.getInstance();
		String key = request.getParameter("key");
		String value = request.getParameter("values");
		ModelAndView mav = new ModelAndView();
		try {
			ph.write(key, value);
			mav.setViewName(folder + "prop");	
		} catch (IOException e) {
			mav.setViewName(folder + "prop_failed");	
		}
		
		return mav;
	}
}
